#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t lock;

void ash(void* argv){
  printf("unlock successful\n");
  pthread_mutex_unlock(&lock);
}

void* fun(void* argv){
  pthread_cleanup_push(ash,NULL);
  int i = 0;
  pthread_mutex_lock(&lock);
  while(i++ < 5){
    printf("hello \n");
    sleep(1);
  }

  pthread_mutex_unlock(&lock);

  pthread_cleanup_pop(0);
  pthread_exit(NULL);
}

int main(int argc,char* argv[]){
  
  pthread_t tid;
  pthread_create(&tid,NULL,fun,NULL);

  sleep(2);
  pthread_cancel(tid);

  pthread_join(tid,NULL);
  pthread_exit(NULL);
}
